<template>
  <el-card class="coins-container">
    <template #header>
      <div class="coins-header">
        <h3>我的虚拟币</h3>
        <el-tag type="success" size="large">
          当前余额: {{ balance }} 虚拟币
        </el-tag>
      </div>
    </template>
    
    <el-tabs v-model="activeTab">
      <el-tab-pane label="充值记录" name="recharges">
        <el-table
          :data="recharges"
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column prop="amount" label="金额" width="120">
            <template #default="{row}">
              +{{ row.amount }} 虚拟币
            </template>
          </el-table-column>
          <el-table-column prop="paymentMethod" label="支付方式" width="120" />
          <el-table-column prop="createdAt" label="充值时间" width="180" />
          <el-table-column prop="status" label="状态" width="100">
            <template #default="{row}">
              <el-tag :type="row.status === 'paid' ? 'success' : 'warning'">
                {{ row.status === 'paid' ? '成功' : '处理中' }}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      
      <el-tab-pane label="消费记录" name="consumptions">
        <el-table
          :data="consumptions"
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column prop="material.title" label="资料名称" />
          <el-table-column prop="coinsSpent" label="消费金额" width="120">
            <template #default="{row}">
              -{{ row.coinsSpent }} 虚拟币
            </template>
          </el-table-column>
          <el-table-column prop="createdAt" label="消费时间" width="180" />
        </el-table>
      </el-tab-pane>
    </el-tabs>
    
    <div class="coins-actions">
      <el-button type="primary" @click="goToRecharge">
        <el-icon><Plus /></el-icon>
        充值虚拟币
      </el-button>
    </div>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getCoinBalance, getRechargeHistory, getConsumptionHistory } from '@/api/user'
import { Plus } from '@element-plus/icons-vue'

const router = useRouter()

const balance = ref(0)
const recharges = ref([])
const consumptions = ref([])
const loading = ref(false)
const activeTab = ref('recharges')

const loadData = async () => {
  try {
    loading.value = true
    
    // 获取余额
    const balanceRes = await getCoinBalance()
    balance.value = balanceRes.data.balance
    
    // 获取充值记录
    const rechargeRes = await getRechargeHistory()
    recharges.value = rechargeRes.data
    
    // 获取消费记录
    const consumptionRes = await getConsumptionHistory()
    consumptions.value = consumptionRes.data
  } catch (error) {
    console.error('获取虚拟币数据失败:', error)
  } finally {
    loading.value = false
  }
}

const goToRecharge = () => {
  router.push('/recharge')
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.coins-container {
  min-height: 500px;
}

.coins-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.coins-actions {
  margin-top: 20px;
  text-align: right;
}
</style>